from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete
from friendship.models import Follow

@receiver(post_save, sender=Follow)
def become_friend(sender, instance, **kwargs):
    # user关注另外一个user，检查是否是好友，如果是，设置两个互相关注对象的is_friend：True。
    print('关注的信号进来了吗？')
    user = instance.user
    who = instance.follow
    print('信号：{0} follow-->> {1}'.format(user, who))
    # follow_me = Follow.objects.filter(user=who, follow=user).first()
    # if follow_me:
    #     print('他也关注我了')
    #     follow_me.is_friend = True
    #     follow_me.save()
    #     instance.is_friend = True
    #     instance.save()


@receiver(post_delete, sender=Follow)
def cancel_friend(sender, instance, **kwargs):
    print('取关的信号进来了吗？')